#include "include.h"


     
int has_next(struct elmer elem, int num) {
    int i, j=0, k=0, l=0, m=0, n=0, o=0, p=0;
    int next[10];
    int next2[10];
    int next3[10];
    int next4[10];
    int next5[10];
    int next6[10];
    int next7[10];
    for(i=0; i<num; i++) {
       if(!match(elem.seq, data[i].seq) && j < 10) next[j++]=i;
       else if(!match8(elem.seq, data[i].seq) && k < 10) next2[k++] = i;
       else if(!match7(elem.seq, data[i].seq) && l < 10) next3[l++] = i;
       else if(!match6(elem.seq, data[i].seq) && m < 10) next4[m++] = i;
       else if(!match5(elem.seq, data[i].seq) && strncmp(elem.seq, data[i].seq, 10) != 0 && n < 10) next5[n++] = i;
       // 
       else if(!strncmp(elem.seq+2, data[i].seq,6) && o < 10) next6[o++] = i;
       //else if(!match4(elem.seq, data[i].seq) && strncmp(elem.seq, data[i].seq, 10) != 0 && o < 10) next6[o++] = i;
       //else if(!match3(elem.seq, data[i].seq) && strncmp(elem.seq, data[i].seq, 10) != 0 && p < 10) next5[p++] = i;
    }
    if(j > 0) return next[rand()%j];
    else if(k > 0) return next2[rand()%k];
    else if(l > 0) return next3[rand()%l];
    else if(m > 0) return next4[rand()%m];
    else if(n > 0) return next5[rand()%n];
    else if(o > 0) return next6[rand()%o];
    //else if(n > 0) return next7[rand()%p];
    else return -1;
}



 
   
void clear(int max) {
     int i=0;
     for(i=0;i<max;i++) data[i].used = 0;
}

     
/* RANDOM */
float solve_rand(int elmer_num, int n) {
    int curr = 0;
    int next = 0;
    int length = 0;
    int best = 0;
    int iterator;
    int used = 0;
    int max_iter = 100;
    int max_len = 0;
    
    for(iterator=0; iterator < max_iter; iterator++) {
        curr = rand()%elmer_num;
        length=0;
        while((next = has_next(data[curr], elmer_num)) >= 0 && data[next].used < 4 && length < n) {
            length++;
            data[curr].used++;
            curr = next;
        }
        used = get_used(elmer_num);
        int u;
        for(u=0;u<elmer_num;u++) data[u].used = 0;
        if(used > best) { best = used; max_len = length; }
        //if((double) best/n > 0.95) break;
    }
    printf("%d\t%d\t%d\t", max_len, best, elmer_num);
    return (float) 100.0*best/elmer_num;
    //return 100.0*best/(elmer_num-50);
}     


int get_n(char *str) {
    int i=0,j=0;
    char CeHaAeR;
    while(str[i] != '.') i++;
    CeHaAeR = str[i+1];
    int result=0;
    result += (((int) CeHaAeR) - 48) * 100;
    result += 9;
    //printf("%d\t", result);
    return result;
}  

int get_err(char *str) {
    int i=0, j=0;
    while(str[i] != '+') i++;
    char x[3];
    x[0] = str[i+1];
    x[1] = str[i+2];
    if((i+3) < strlen(str)) x[3]=str[i+3]; else x[2] = '0';
    int result=0;
    
    return result;
}
       
// pozytywne losowe:  9.200+80 18.200+80 35.200+80 20.300+120 55.300+120 58.300+120 55.400+160 62.400+160 68.400+160 10.500+200 25.500+200 53.500+200
// pozytywne przeklamania na koncach:  9.200+20 18.200+20 35.200+20 20.300+30 55.300+30 58.300+30 55.400+40 62.400+40 68.400+40 10.500+50 25.500+50 53.500+50
       
// powtorzenia:  59.500-2 113.500-8 144.500-12 28.500-18 34.500-32
// losowe:      9.200-40 9.200-80 18.200-40 18.200-80 35.200-40 35.200-80 20.300-60 20.300-120 55.300-60 55.300-120 58.300-60 58.300-120 55.400-80 55.400-160 62.400-80 62.400-160 68.400-80 68.400-160 10.500-100 10.500-200 25.500-100 25.500-200 53.500-100 53.500-200
int main(int argc, char *argv[])
{
   if(argc < 2) exit(-1);
   else {
       int i, elmer_num;
       printf("INSTANCE\tn\tlength\tused\tl-mers\teff\n");
       int bledy=0;
       for(i=1; i<argc; i++) {
           srand(time(NULL));
           
           printf("%s\t", argv[i]);
           if((elmer_num = wczytaj(argv[i])) == -1) continue;
           
           printf("%d\t", get_n(argv[i]));
           float acc = solve_rand(elmer_num, get_n(argv[i]));
           printf("%.2f\n", acc);
           
           
           bledy+=acc;
           
           /*
           float acc2 = solve_nextlist(elmer_num, get_n(argv[i]));
           printf("%.2f\n", acc2);
           
           float acc3 = solve_rand(elmer_num, get_n(argv[i]));
           printf("%.2f\t", acc3);
           
           printf("%.2f\n", (acc+acc2+acc3)/3.0);*/
           }
           printf("bLEDY = %f", (float) bledy/(argc-1));
       }
       
  system("PAUSE");	
  return 0;
}
